﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Web;

namespace Common
{
    public class FileConvert_SWF
    {
        /// <summary>
        /// 文档转swf
        /// </summary>
        /// <param name="inFilename"></param>
        /// <param name="swfFilename"></param>
        public static bool ConvertPdfToSwf(string inFilename)
        {
            bool isStart;
            string SwfFile = strType(inFilename);//转换后的文件后缀名
            string swfFilename = inFilename.Replace(SwfFile, ".swf");
            Process process = new Process();
            try
            {
                string flashPrinter = HttpContext.Current.Server.MapPath("~/FlashPrinter/FlashPrinter.exe");
                FileInfo fileinfo = new FileInfo(inFilename);
                ProcessStartInfo startInfo = new ProcessStartInfo(flashPrinter);
                int time = ((int)(fileinfo.Length / (1024 * 1204))) * 20 + 20;
                startInfo.Arguments = string.Concat(inFilename, " -o ", swfFilename);
                startInfo.UseShellExecute = false;//不使用系统外壳程序启动
                startInfo.RedirectStandardInput = false;//不定向输入
                startInfo.RedirectStandardOutput = false;//不定向输出
                startInfo.CreateNoWindow = true;//不创建CMD窗口
                startInfo.RedirectStandardError = true;
                startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                process.StartInfo = startInfo;
                isStart = process.Start();
                process.WaitForExit(time * 1000);
                process.Close();
                //process.Kill();
                process.Dispose();

                //while (!File.Exists(swfFilename))
                //{
                //    Thread.Sleep(1);
                //}
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (process != null)
                {

                    process.Close();

                }
            }
            return isStart;
        }

        /// <summary>
        /// 检查上传文件后缀名
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string strType(string type)
        {
            if (String.Empty != type)
            {
                type = type.ToLower();
                type = type.Substring(type.LastIndexOf("."));
                if ((".swf,.flv").Contains(type))
                    return type;

                if ((".doc,.docx,.wps").Contains(type))
                    return type;

                if ((".jpg").Contains(type))
                    return type;

                if ((".pdf").Contains(type))
                    return type;

                if ((".xls,.xlsx").Contains(type))
                    return type;

                if ((".txt").Contains(type))
                    return type;

                if ((".bmp").Contains(type))
                    return type;

                if ((".ppt,.pptx").Contains(type))
                    return type;
            }
            return type;
        }
    }
}
